package com.ruoyi.web.service.warning.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.StateConstants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.domain.warning.FactorInfo;
import com.ruoyi.web.domain.warning.WarningConfig;
import com.ruoyi.web.mapper.warning.WarningConfigMapper;
import com.ruoyi.web.pojo.request.warning.WarningConfigRequest;
import com.ruoyi.web.service.warning.FactorInfoService;
import com.ruoyi.web.service.warning.FactorMappingService;
import com.ruoyi.web.service.warning.WarningConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 五情-传感器-预警配置表 服务实现类
 * @author fwx
 * @date 2024-01-30 15:08:07
 */
@Service
public class WarningConfigServiceImpl extends ServiceImpl<WarningConfigMapper, WarningConfig> implements WarningConfigService {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private FactorInfoService factorInfoService;

    /**
    * 新增五情-传感器-预警配置表
    */
    @Override
    public void insert(WarningConfig config){

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        if(loginUser == null){
            throw new RuntimeException("用户未登录");
        }

        config.setDeptId(loginUser.getDeptId());
        config.setUserId(loginUser.getUserId());
        config.setUsername(loginUser.getUsername());

        config.setCrtTime(LocalDateTime.now());
        config.setIsEnable(StateConstants.ENABLE);
        super.save(config);
    }

    /**
    * 修改五情-传感器-预警配置表
    */
    @Override
    public void update(WarningConfig WarningConfig){
        super.updateById(WarningConfig);
    }

    @Override
    public void saveOrUpdateInfo(WarningConfig config){

        if(config.getId() != null){
            this.update(config);
            return;
        }

        //判断重复的策略 TODO
        insert(config);
    }
    @Override
    public void updateEnable(Integer id, Integer flag){

        //处理错误的值
        if (!Objects.equals(flag,StateConstants.ENABLE)){
            flag = StateConstants.DEACTIVATE;
        }

        UpdateWrapper<WarningConfig> wrapper = new UpdateWrapper<>();
        wrapper.lambda().set(WarningConfig::getIsEnable,flag)
                .eq(WarningConfig::getId,id)
        ;

        super.update(wrapper);
    }

    /**
    * 删除五情-传感器-预警配置表
    */
    @Override
    public void delete(String id){
        super.removeById(id);
    }

    /**
    *  分页查询五情-传感器-预警配置表
    */
    @Override
    public void queryPage(IPage<WarningConfig> page){
        QueryWrapper<WarningConfig> query  = new QueryWrapper<>();
        super.page(page,query);
    }

    @Override
    public List<WarningConfig> queryPage(WarningConfigRequest request){
        QueryWrapper<WarningConfig> query  = new QueryWrapper<>();
        request.bulidWrapper(query);

        if(StringUtils.isNotBlank(request.getFactorType())){
            List<FactorInfo> factorInfos = factorInfoService.getByType(request.getFactorType());
            if(factorInfos.size() > 0){
                List<String> keys = factorInfos.stream().map(FactorInfo::getFactorKey).collect(Collectors.toList());
                query.lambda().in(WarningConfig::getFactorKey,keys);
            }
        }

        List<WarningConfig> list = super.list(query);
        return list;
    }

    /**
    * 查询所有五情-传感器-预警配置表
    */
    @Override
    public List<WarningConfig> queryAll(){
        QueryWrapper<WarningConfig> query  = new QueryWrapper<>();
        List<WarningConfig> list = super.list(query);
        return list;
    }

    /**
    * 根据id查询五情-传感器-预警配置表
    */
    @Override
    public WarningConfig findById(String id){
        WarningConfig res = super.getById(id);
        return res;
    }

}
